/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */

package cgl.narada.aggregator;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import cgl.narada.protocol.DataReceived;
import cgl.narada.protocol.Handler;
import cgl.narada.transport.TransportException;
import cgl.narada.transport.TransportHandler;
import cgl.narada.transport.TransportHandlerImpl;


/** This is an example which demonstrates using the transport interfaces in
    Narada. This class has presently been written for TCP communications.
    Subsequent versions of this class would include support for other
    transport types supported in NaradaBrokering.

    @author Shrideep Pallickara
    $Date$
    $Revision$
*/

public class AggregationNode implements Handler, InterchangeConstants {

  private Hashtable notifiers;
  private String moduleName = "AggregationNode: ";
  private String [][] metrics = new String [12][8];
  private String node_ID="";
  private int count=0;

  private int number_of_metrics=8;
  private int number_of_total_links=12, allowedUserLinkNumber=0;

  private boolean first=true, userFirst=true;
  /** Deals with the transport related aspects of communication */
  private TransportHandler transportHandler;
  long  pos=0, posUser=0;

  public AggregationNode() {
    transportHandler = new TransportHandlerImpl(this);
    notifiers = new Hashtable();
    //initialize metrics
    for (int i=0; i<number_of_total_links;i++){
      for (int k=0; k<number_of_metrics;k++){
        metrics[i][k]="NA";
      }
    }
  
  }

  public void loadCommunicationsOfType(Properties commProps, String commType) {
    try {
      transportHandler.loadCommunicationsOfType(commProps, commType);
    } catch (TransportException transEx) {
      System.out.println(transEx);
    }
  }

  public String createLinkTo(Properties props, String linkType) {
    try {
      return transportHandler.setupLink(props, linkType);
    } catch (TransportException transEx) {
      System.out.println(transEx);
    }
    return null;
  }


  public void sendDataTo(byte[] data, String node) throws TransportException {
    transportHandler.sendData(data, node);
  }

  /** This method indicates that the node in question is a client */
  public boolean isClient() {
    return false;
  }

  public void requestInfoFromNotifiers() {
    byte[] requestInfo = new byte[1];
    requestInfo[0] = INFO_REQUEST_FROM_AGGREGATOR;
    if (notifiers.size() == 0) {
      System.out.println(moduleName + "No registered notifiers :( ");
    }
    Enumeration e = notifiers.keys();
    while (e.hasMoreElements()) {
      String notifierKey = (String) e.nextElement();
      try {
	sendDataTo(requestInfo, notifierKey);
      } catch (TransportException transEx) {
	System.out.println(transEx);
	notifiers.remove(notifierKey);
	System.out.println(moduleName + "Removed [" + notifierKey +
			   "] from the list of managed notifiers");
      } /*end try-catch */
    } /*end while*/

  }



  public void handleData(byte[] data, String sender) {
   
    node_ID=sender.substring(0, sender.indexOf(":"));

    if (data.length == 0) {
      System.out.println(moduleName + "Data received has length = 0");
      return;
    }
    int _dataInfo = (int) data[0];

    switch(_dataInfo) {
    case (int) REGISTER_NOTIFIER:
      if (notifiers.containsKey(sender)) {
	System.out.println(moduleName + "Notifier was already registered!!");
	break;
      } /*end if*/
      notifiers.put(sender, sender);
      System.out.println(moduleName + "Registered notifier [" + sender + "]");
      break;

    case (int) DEREGISTER_NOTIFIER:
      if (notifiers.containsKey(sender)) {
	notifiers.remove(sender);
	System.out.println(moduleName +
			   "Deregistered notifier [" + sender + "]");
	break;
      }
      System.out.println(moduleName + "Unaware of notifer being deregistered ["
			 + sender + "]");
      break;

    case (int) INFO_RESPONSE_FROM_NOTIFIER:
      String _response = new String(data, 1, data.length -1);
      System.out.println(moduleName +"Received response <<<" + _response +
			 " >>> from notifier [" + sender +"]");
      break;

    case (int) INFO_POST_FROM_NOTIFIER:
      String _post = new String(data, 1, data.length -1);

      System.out.println(moduleName +"Received info POST <<<" + _post +
			 " >>> from notifier [" + sender +"]");
      //processXML(_post);
      //convert it into a dom abject.
      SAXBuilder builder = new SAXBuilder();
      Document doc = null;
      try {
        doc = builder.build(new StringReader(_post));
      }
      catch (IOException ex) {
      }
      catch (JDOMException ex) {
      }
      try{
        XMLOutputter out=new XMLOutputter();
        out.output(doc,System.out);
      }catch(Exception e){
        System.err.println(e);
      }//end try

      Element root=doc.getRootElement();
      if ((root.getChildText("user_group")).equalsIgnoreCase("U")){
        writeUserXML(_post);
      }
      writeAdminXML(_post);
      break;


    default:
      System.out.println(" Data[0] ->" + _dataInfo +
			 " Data length = " + data.length);
      break;
    }
  }

  //check the status of the links by using xpath queries.
  public void checkStatus(){
    long starttime,endtime;
    XmlMatching check=new XmlMatching();
    starttime=System.currentTimeMillis();
    System.out.println("start time"+starttime);
    check.checkLatency("C:\\test\\adminXML.xml", "/P_Metrics/performancedata[last()][avg_latency>100]");
    endtime=System.currentTimeMillis();
    System.out.println("end time"+endtime);

    check.jitter("C:\\test\\adminXML.xml", "/P_Metrics/performancedata[last()][jitter>100]");
    check.throughput("C:\\test\\adminXML.xml", "/P_Metrics/performancedata[last()][throughput<50]");
    check.dataRate("C:\\test\\adminXML.xml", "/P_Metrics/performancedata[last()][dataRate<1.5]");
  }

  public void processXML(String xml){
    String Values []={"","","","","","","","",""};
    int index=0;
    Values[0]= xml.substring(xml.indexOf("<Node_ID>")+9, xml.indexOf("</Node_ID>") );
    Values[1]= xml.substring(xml.indexOf("<link_ID>")+9, xml.indexOf("</link_ID>") );
    Values[2]= xml.substring(xml.indexOf("<data_rate>")+11, xml.indexOf("</data_rate>") );
    Values[3]= xml.substring(xml.indexOf("<avg_latency>")+13, xml.indexOf("</avg_latency>") );
    Values[4]= xml.substring(xml.indexOf("<max_delay>")+11, xml.indexOf("</max_delay>") );
    Values[5]= xml.substring(xml.indexOf("<min_latency>")+13, xml.indexOf("</min_latency>") );
    Values[6]= xml.substring(xml.indexOf("<jitter>")+8, xml.indexOf("</jitter>") );
    Values[7]= xml.substring(xml.indexOf("<message_rate>")+14, xml.indexOf("</message_rate>") );
    Values[8]= xml.substring(xml.indexOf("<throughput>")+12, xml.indexOf("</throughput>") );
    if (Values[0].equalsIgnoreCase("Sierra:443") && metrics[0][1].equalsIgnoreCase("NA")){
      metrics[0][1]=Values[1];
      }
      else if (Values[0].equalsIgnoreCase("Sierra:443") && metrics[1][1].equalsIgnoreCase("NA")){
         metrics[1][1]=Values[1];
         }
         else if (Values[0].equalsIgnoreCase("Sierra:443") && metrics[2][1].equalsIgnoreCase("NA")){
            metrics[2][1]=Values[1];
            }
            else if (Values[0].equalsIgnoreCase("Sierra:443") && metrics[3][1].equalsIgnoreCase("NA")){
               metrics[3][1]=Values[1];
               }
               else if (Values[0].equalsIgnoreCase("Sierra:443") && metrics[4][1].equalsIgnoreCase("NA")){
                  metrics[4][1]=Values[1];
                  }
                  else if (Values[0].equalsIgnoreCase("Sierra:443") && metrics[5][1].equalsIgnoreCase("NA")){
                     metrics[5][1]=Values[1];
                     }
      else if (Values[0].equalsIgnoreCase("Ger:443") && metrics[6][1].equalsIgnoreCase("NA")){
            metrics[6][1]=Values[1];
            }
            else if (Values[0].equalsIgnoreCase("Ger:443") && metrics[7][1].equalsIgnoreCase("NA")){
                     metrics[7][1]=Values[1];
            }
            else if (Values[0].equalsIgnoreCase("Ger:443") && metrics[8][1].equalsIgnoreCase("NA")){
                     metrics[8][1]=Values[1];
            }
            else if (Values[0].equalsIgnoreCase("Ger:443") && metrics[9][1].equalsIgnoreCase("NA")){
                     metrics[9][1]=Values[1];
            }
            else if (Values[0].equalsIgnoreCase("Ger:443") && metrics[10][1].equalsIgnoreCase("NA")){
                     metrics[10][1]=Values[1];
            }
            else if (Values[0].equalsIgnoreCase("Ger:443") && metrics[11][1].equalsIgnoreCase("NA")){
                     metrics[11][1]=Values[1];
            }


/*
//assigning link IDs to metrics array.
    for (int i=0; i<number_of_total_links; i++){

      if (Values[0].equalsIgnoreCase("Sierra:443") && metrics[i][1].equalsIgnoreCase("NA")){
        metrics[i][1]=Values[1];
        break;
      }
      else if (Values[0].equalsIgnoreCase("Ger:443") && metrics[i][1].equalsIgnoreCase("NA")){
        metrics[i][1]=Values[1];
        break;
      }

    }
*/




//finding the index
    for (int i=0; i<number_of_total_links;i++){
      if (Values[1].equals(metrics[i][1])){index=i;}
    }
//assigning values to the metrics
    for (int i=0; i<9; i++){
       metrics[index][i]=Values[i];
    }

    writeHtml(Values);//write files into an HTML file

}//end process XML








//Writes an XML messages into a file.
  public void writeAdminXML(String xmlmetrics){
   try{
     //open an randomaccess file adminXML.xml
     RandomAccessFile XMLFile = new RandomAccessFile("c:\\test\\adminXML.xml", "rw");
     //System.out.println("POS>>>>"+pos);
     XMLFile.seek(pos); //position the pointer to the end of the file.
     if (first) { //this first one is different because we want header information to appear only once.
       String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><P_Metrics>"+xmlmetrics.substring( (xmlmetrics.indexOf(
           "<performancedata>"))) + "</P_Metrics>";
       byte[] by = s.getBytes();
       XMLFile.write(by);
       pos=s.length()-12;

     }else{

       String without_top_element = xmlmetrics.substring( (xmlmetrics.indexOf(
           "<performancedata>"))) + "</P_Metrics>"; //adding the root element at the end of the file/

       byte[] by1 = without_top_element.getBytes();
       //XMLFile.writeChars(without_top_element);
       XMLFile.write(by1);
       pos = pos + without_top_element.length()-12; //position the pointer so that root element will be erased next time
     }
     first = false;
     XMLFile.close();
   }catch(IOException e){
     System.out.println("Cannot Create The File");
   }

  }//end writeAdminXML


/*
//writes the xmls into flat file in append mode
public void writeXML(String xmlmetrics){
 try{
   PrintWriter out = new PrintWriter(new FileWriter("c:\\test\\xmlfile.xml", true));
   if (first) {
     out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
   }
   first=false;
   String without_top_element=xmlmetrics.substring((xmlmetrics.indexOf("<performancedata>")));
   out.println(without_top_element);
   out.close();
 }catch(IOException e){
   System.out.println("Cannot Create The File");
 }

}//end writeXML
*/



//writes the xmls for User view into flat file
public void writeUserXML(String xmlmetrics){
  try{
    //open an randomaccess file adminXML.xml
    RandomAccessFile XMLFile = new RandomAccessFile("c:\\test\\userXML.xml", "rw");
    System.out.println("POS>>>>"+posUser);
    XMLFile.seek(posUser); //position the pointer to the end of the file.
    if (userFirst) { //this first one is different because we want header information to appear only once.
      String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><P_Metrics>"+xmlmetrics.substring( (xmlmetrics.indexOf(
          "<performancedata>"))) + "</P_Metrics>";
      byte[] by = s.getBytes();
      XMLFile.write(by);
      posUser=s.length()-12;

    }else{

      String without_top_element = xmlmetrics.substring( (xmlmetrics.indexOf(
          "<performancedata>"))) + "</P_Metrics>"; //adding the root element at the end of the file/

      byte[] by1 = without_top_element.getBytes();
      //XMLFile.writeChars(without_top_element);
      XMLFile.write(by1);
      posUser = posUser + without_top_element.length()-12; //position the pointer so that root element will be erased next time
    }
    userFirst = false;
    XMLFile.close();
  }catch(IOException e){
    System.out.println("Cannot Create The File");
  }

}//end writeUserXML






  public void writeHtml(String values[]){
    try{
      PrintWriter out = new PrintWriter(new FileWriter("c:\\test\\report.html"));
      out.println("<HTML>");
      //out.println("<INPUT TYPE=SUBMIT VALUE='Refresh' class='normal'>");
      out.println("<table border='0' cellspacing='1' cellpadding='0' width='100%' bgcolor='#0000ff'>");
      out.println("<tr><td align='center'><table border='0' cellspacing='1' cellpadding='1' width='100%' bgcolor='#ffffff'><tr>");
      out.println("<td align='center' class='headerW' bgcolor='#777777'>Link</td>");
      out.println("<td align='center' class='headerW' bgcolor='#33FF66'>data rate(KB/sec)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#33FF66'>avg. latency(ms)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#33FF66'>min. latency(ms)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#33FF66'>max. latency(ms)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#33FF66'>jitter(ms)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#33FF66'>message rate(msg/sec)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#33FF66'>throughput(KB/sec)</td></tr>" );
      out.println("</tr>");
      for (int i=0; i<number_of_total_links; i++){
        if (metrics[i][1].length()>15){
          out.println("<tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[i][0]+"ssl/=>Link"+metrics[i][1].substring(15)+"</td>");
        }else
          out.println("<tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[i][0]+"ssl/=>Link"+metrics[i][1]+"</td>");

        out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[i][2]+"</td>");
        out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[i][3]+"</td>");
        out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[i][4]+"</td>");
        out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[i][5]+"</td>");
        out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[i][6]+"</td>");
        out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[i][7]+"</td>");
        out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[i][8]+"</td>");
      }

/*
      out.println("value...:"+values[0]);
      out.println("value...:"+values[1]);
      out.println("value...:"+values[2]);
      out.println("value...:"+values[3]);
      out.println("value...:"+values[4]);
      out.println("value...:"+values[5]);
      out.println("value...:"+values[6]);
      out.println("value...:"+values[7]);
*/




      out.println("</HTML>");
      out.close();
    }catch(IOException e){
      System.out.println("Cannot Create The File");
    }





  }//end of writeHTML

  public static void main(String[] args) {
    if (args.length !=1) {
      System.out.println("Usage: java cgl.narada.aggregator.AggregationNode "+
			 "<aggregator-port>");
      System.exit(0);
    }

    AggregationNode aggregator = new AggregationNode();
    Properties props = new Properties();
    props.put("TCPServerPort", args[0]);
    aggregator.loadCommunicationsOfType(props, "tcp");


    BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
   
    String cmd;
    System.out.println("\n Type h for help and usage indicators ... ");
    while (true) {
      try {
	cmd = r.readLine();
	if (cmd == null) {
	  break;
	}

	if(cmd.startsWith("h")) {
	  System.out.println("Request Info from Notifiers       -> ri");
	}

	if (cmd.startsWith("ri")) {
	  aggregator.requestInfoFromNotifiers();
	  continue;
	} /** end request info */


      }catch (IOException ioe) {
	ioe.printStackTrace();
      } /* end try-catch */
    } /* end while(true) */
    System.out.println("AggregatorNode shutting down services ....");
  }/*end main() */

}



/*
  public void writeHtml(String values[]){
    try{
      PrintWriter out = new PrintWriter(new FileWriter("c:\\test\\report.html"));
      out.println("<HTML>");
      //out.println("<INPUT TYPE=SUBMIT VALUE='Refresh' class='normal'>");
      out.println("<table border='0' cellspacing='1' cellpadding='0' width='100%' bgcolor='#0000ff'>");
      out.println("<tr><td align='center'><table border='0' cellspacing='1' cellpadding='1' width='100%' bgcolor='#ffffff'><tr>");
      out.println("<td align='center' class='headerW' bgcolor='#777777'>Link</td>");
      out.println("<td align='center' class='headerW' bgcolor='#FFFFFF'>data rate(KB/sec)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#FFFFFF'>avg. latency(ms)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#FFFFFF'>min. latency(ms)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#FFFFFF'>max. latency(ms)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#FFFFFF'>jitter(ms)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#FFFFFF'>message rate(msg/sec)</td>");
      out.println("<td align='center' class='headerW' bgcolor='#FFFFFF'>throughput(KB/sec)</td></tr>" );
      out.println("</tr>");
      if (metrics[0][1].length()>15){
      out.println("<tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[0][0]+"ssl/=>Link"+metrics[0][1].substring(15)+"</td>");
      }else
        out.println("<tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[0][0]+"ssl/=>Link"+metrics[0][1]+"</td>");

      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[0][2]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[0][3]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[0][4]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[0][5]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[0][6]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[0][7]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[0][8]+"</td>");
      if (metrics[1][1].length()>15){
      out.println("</tr><tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[1][0]+"ssl/=>Link"+metrics[1][1].substring(15)+"</td>");
      }else
        out.println("</tr><tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[1][0]+"ssl/=>Link"+metrics[1][1]+"</td>");

      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[1][2]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[1][3]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[1][4]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[1][5]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[1][6]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[1][7]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[1][8]+"</td>");
      if (metrics[2][1].length()>15){
      out.println("</tr><tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[2][0]+"ssl/=>Link"+metrics[2][1].substring(15)+"</td>");
      }else
        out.println("</tr><tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[2][0]+"ssl/=>Link"+metrics[2][1]+"</td>");

      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[2][2]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[2][3]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[2][4]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[2][5]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[2][6]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[2][7]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[2][8]+"</td>");
      if (metrics[3][1].length()>15){
      out.println("</tr><tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[3][0]+"ssl/=>Link"+metrics[3][1].substring(15)+"</td>");
      }else
        out.println("</tr><tr><td align='left' class='normal' bgcolor='#999999'>"+"Host=>"+metrics[3][0]+"ssl/=>Link"+metrics[3][1]+"</td>");

      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[3][2]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[3][3]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[3][4]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[3][5]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[3][6]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[3][7]+"</td>");
      out.println("<td align='center' class='normal' bgcolor='#aaaaaa'>"+metrics[3][8]+"</td>");
      out.println("</tr></table></td></tr></table></p>");

/*
      out.println("value...:"+values[0]);
      out.println("value...:"+values[1]);
      out.println("value...:"+values[2]);
      out.println("value...:"+values[3]);
      out.println("value...:"+values[4]);
      out.println("value...:"+values[5]);
      out.println("value...:"+values[6]);
      out.println("value...:"+values[7]);





      out.println("</HTML>");
      out.close();
    }catch(IOException e){
      System.out.println("Cannot Create The File");
    }





  }
*/
